package Test;

import DB.ConnectionDB;
import DB.ProductDB;
import Model.Product;
import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.fail;

public class ProductDBTest {

    @Test
    public void testFindProduct() throws Exception {

        ProductDB productDB = new ProductDB();

        int find = 6;

        Product product = productDB.findProduct(find);

        if (product != null) {
            System.out.println(product.getId() + " " +
                    product.getName() + " " +
                    product.getPurchasePrice() + " " +
                    product.getSalesPrice() + " " +
                    product.getRentPrice() + " " +
                    product.getCountryOfOrigin() + " " +
                    product.getMinStock() + " " +
                    product.getType());
        }
        else {
            fail("Error finding product - product is null");
        }
    }

    @Test
    public void testInsertProduct() throws Exception {

        ProductDB productDB = new ProductDB();
        int rc = productDB.insertProduct(new Product(100, "Sheriff stjerne" ,50,150,35, "Denmark", 200, "Equipment", 1));

        System.out.println(rc);

        if (rc == 1) {
            System.out.println("Inserting successful");
        }
        else {
            fail("Failed inserting product");
        }
    }

    @Test
    public void testGetAllProducts() throws Exception {
        ProductDB productDB = new ProductDB();
        ArrayList<Product> proObj = productDB.getAllProducts(false);

        if(proObj != null) {
            for (Product p : proObj) {
                System.out.println(p.getName());
            }
        }
        else {
            fail("No products found");
        }
    }

    @Test
    public void testFindProduct1() throws Exception {
        ProductDB productDB = new ProductDB();
        ArrayList<Product> product = productDB.findProducts("cowboy", true);


        for (Product p : product)
        if (product != null) {
            System.out.println(p.getName());
        }
        else {
            fail("Failed finding product by name");
        }
    }
}
